const app = getApp()
import Toast from '../vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showbar:false,
    page:0,
    coupon:[],
    store:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getParam(options,(param) => {
      if(app.util().isNull(param.store_id)){
        wx.switchTab({
          url: '/tab/index',
        })
      }else{
        this.data.store_id = param.store_id
        app.getLbs((rel)=>{
          this.couponApi();
        })
      }
    })    
  },
  //优惠券API
  couponApi: function () {
    app.api().Get('api/v1/ais/storepay/couponTop',{
      store_id:this.data.store_id,
    },(result) => {
      if (result.code == 200) {
        this.setData({
          coupon:result.data.coupon,
          store:result.data.store,
        })
      }else{
        wx.switchTab({
          url: '/tab/index',
        })
      }
    })
  },
  //领取优惠券
  onCoupon: function (e) {
    Toast.loading({message: '加载中'});
    setTimeout(() => {
      wx.hideLoading();
    },1000)
    var coupon_key = e.currentTarget.dataset.key;
    var coupon = this.data.coupon;
    app.api().Post('api/v1/ais/couponuser/getCoupon',{id: e.currentTarget.id},(rel) => {
      Toast.success('领取成功');
      coupon[coupon_key]['is_get_coupon'] = 1;
      this.setData({
        coupon: coupon,
      })
    })
  },
  //滚动
  onPageScroll: function (res){
    let scrollTop = res.scrollTop;
    this.setData({
      showbar: 80 <= scrollTop ? true :false
    })
    wx.setNavigationBarColor({
      frontColor:80 <= scrollTop ? '#000000' :'#ffffff',
      backgroundColor: '#ffffff'
    })
  },
  //查看优惠券规则
  reCoupon:function(e){
    wx.navigateTo({
      url:e.currentTarget.dataset.url,
      fail: (res) => {
        app.wxAlert('未找到页面')
      }
    })
  },
  //返回首页
  onClickLeft:function(e){
    wx.switchTab({
      url: '/tab/index',
    })
  }
})